package com.lanou3g.dllo.baidumusic.musiclibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.musiclibrary.mv.MVFragment;
import com.lanou3g.dllo.baidumusic.musiclibrary.playlist.PlaylistFragment;
import com.lanou3g.dllo.baidumusic.musiclibrary.radio.RadioFragment;
import com.lanou3g.dllo.baidumusic.musiclibrary.ranklist.RanklistFragment;
import com.lanou3g.dllo.baidumusic.musiclibrary.recommend.RecommendFragment;

import java.util.ArrayList;

/**
 * Created by dllo on 16/6/17.
 */
public class MusicLibraryFragment extends BaseFragment {

    private TabLayout musicLibrary_tabLayout;
    private ViewPager musicLibraryViewPager;
    private MusicLibraryAdapter musicLibraryAdapter;
    private ArrayList<Fragment> datas;
    private MusicLibraryBroadCast musicLibraryBroadCast;

    @Override
    public int setLayout() {
        return R.layout.fragment_musiclibrary;
    }

    @Override
    public void initView(View view) {

        musicLibrary_tabLayout = (TabLayout) view.findViewById(R.id.musiclibrary_tablayout);
        musicLibraryViewPager = (ViewPager) view.findViewById(R.id.musiclibrary_viewpager);

    }

    @Override
    public void initData() {

        musicLibraryBroadCast = new MusicLibraryBroadCast();
        IntentFilter filter = new IntentFilter();
        filter.addAction(context.getPackageName() + ".TOPLAYLISTFRAGMENT");
        context.registerReceiver(musicLibraryBroadCast, filter);

        datas = new ArrayList<>();
        datas.add(new RecommendFragment());
        datas.add(new RanklistFragment());
        datas.add(new PlaylistFragment());
        datas.add(new RadioFragment());
        datas.add(new MVFragment());

        musicLibraryAdapter = new MusicLibraryAdapter(getFragmentManager());
        musicLibraryAdapter.setDatas(datas);
        musicLibraryViewPager.setAdapter(musicLibraryAdapter);
        musicLibrary_tabLayout.setupWithViewPager(musicLibraryViewPager);
        musicLibrary_tabLayout.setTabTextColors(Color.GRAY, getResources().getColor(R.color.colorBlue));
        // 设置跟随条的颜色
        musicLibrary_tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorBlue));

    }

    class MusicLibraryBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            musicLibraryViewPager.setCurrentItem(2);
        }
    }

    @Override
    public void onDestroy() {
        context.unregisterReceiver(musicLibraryBroadCast);
        super.onDestroy();
    }
}
